﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Project_01
{
    public struct UserInfo {
        public String username;
        public String password;
        public String gender;
        public UserInfo(String username, String password, String gender) {
            this.username = username;
            this.password = password;
            this.gender = gender;
        }
    }
    public partial class MainForm : Form
    {
        Database database;
        public MainForm()
        {
            InitializeComponent();
            try {
                database = new Database("", "");
                UpdateUserlist("");
            } catch (Exception e) {
                MessageBox.Show(e.Message);
            }
        }
        private void UpdateUserlist(String filter) {
            listView1.Items.Clear();
            listView1.BeginUpdate();
            List<String> list = database.GetUsername(filter);
            for (Int32 index = 0; index < list.Count; ++index) {
                listView1.Items.Add(list[index]);
            }
            listView1.Columns[listView1.Columns.Count - 1].Width = listView1.Width - 21;
            listView1.EndUpdate();
        }
        private bool CloseForm() {
            return true;
            //MessageBox.Show("Are you sure you want to exit?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
        }
        private void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConnectForm form = new ConnectForm();
            form.ShowDialog(this);
        }
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            UpdateUserlist(textBox1.Text);
        }
        private void MainForm_FormClosing(Object sender, FormClosingEventArgs e)
        {
            e.Cancel = !CloseForm(); 
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            UserInfo info = database.GetUserInfo(listView1.SelectedItems[0].Text);
            MessageBox.Show(info.username + info.password + info.gender);
        }
    }
}
